package com.android.demo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActivityGroup;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;
import android.media.ExifInterface;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Window;
import android.widget.LinearLayout;

public class TestLoadJsonFromAssert extends ActivityGroup {

	/** Called when the activity is first created. */
	private final static String  TAG = "TestLoadJsonFromAssert";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(new LinearLayout(this));

		testLoadJson();
	}
	
	public int getDegree(String filename){
		int result = 0; 
		int orientation = 0;
		try {
			ExifInterface exif = new ExifInterface(filename);	
			orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
			
		}catch (Exception e) {
		}
		
		switch (orientation) {
		case ExifInterface.ORIENTATION_ROTATE_90:
			result = 90;
			break;
		case ExifInterface.ORIENTATION_ROTATE_180:
			result = 180;
			break;
		case ExifInterface.ORIENTATION_ROTATE_270:
			result = 270;
			break;
		}
		
		return result;
	}
	
	public void testLoadJson(){
		byte[] byteJson = getByteArrayFromAsserts("citys.json");
		String str = new String(byteJson);
		
		try {
			JSONObject jsonObj = new JSONObject(str);
//			JSONArray jsonArray = new JSONArray(str);
			Log.i(TAG, "Json length"+jsonObj.length());
		}catch(JSONException e){
			Log.e(TAG, "Json length"+e.toString());
		}
		
	}
	
	//1 get byte from asserts
	public byte[] getByteArrayFromAsserts(String fileName){
		InputStream inputStream = null;
		byte[] result = null;
		try {
			inputStream = getAssets().open(fileName);
			
			int size = inputStream.available();
			result = new byte[size];
			
			inputStream.read(result);
			
		}catch (IOException e) {
			Log.i(TAG, "getAssertFromAsserts" + e.toString());
		}
		
		finally{
			try {
				if (inputStream!=null){
					inputStream.close();
				}
			}catch(IOException e) {
				Log.i(TAG, "getAssertFromAsserts" + e.toString());
			}
		}
		
		return result;
	}
}
